// @ts-nocheck
import Log from '../../utils/log.js'
const lg = new Log();
export default {
  data(){
    return {
      time_title: "",  //标题
      dayList:[],  // 显示的日历列表
      isSelectDay: null,  // 点击选中的时间
      today: '',   // 今天的时间
      currentYM: '',  // 当前年月
      type: '',
    }
  },
  props: {
    //被标记的日期markDay
    markDay:{
      type: Array,
      default: [],
    },
    //是否是周一开始,
    mondayStart:{
      type: Boolean,
      default: false,
    },
    //日历头文字
    weekList: {
      type: Array,
      default: ['日','一','二','三','四','五','六']
    }
  },
  onPageShow(){
  },
  onLayoutReady(){
  },
  onAttached(){
  },
  onInit() {
    this.initDay(this.currentYM);
  },
  onShow(){
  },

  cancel(){
    this.$emit('cancel')
  },
  confirm(){
    this.$emit('confirm', this.isSelectDay || this.today)
  },

  // 跳转到今天
  toToday(){
    this.isSelectDay = this.today;
    this.currentYM = this.today;
    this.initDay(this.currentYM);
  },
  goDay(day){
    this.initDay(day);
  },

  // 点击选择日期
  selectDay(e){
    if(!e) return;
    if(e.label == 'last'){
      this.cutMonth(-1);
    }
    if(e.label == 'next'){
      this.cutMonth(1);
    }
    this.isSelectDay = e.time;
//    lg.log(JSON.stringify(this.isSelectDay));

    this.$emit('selectDay', this.isSelectDay);

  },

  /**
   * 初始化时间
   * @param day:string , 需要显示初始化日历的年月，格式：y-M
   */
  initDay(day){
    this.today = this.formatDate(new Date(),'y-M-d');
    if(!day){
      day = this.today;
    }
    lg.log('获取今天的时间：',day,this.today);
    this.getCld(day);

  },

  setWeekList(arr){
    arr.push(arr[0]);
    arr.splice(0,1);
    return arr
  },
  // 获取日历
  getCld(time){
    let dd = new Date(time);
    let {year: y, month: m, monthLen} = this.getDateInfo(time);
    let {year: l_y, month: l_m, monthLen: l_monthLen} = this.getDateInfo(time,-1);
    let {year: n_y, month: n_m} = this.getDateInfo(time,+1);
//    lg.warn(`当前年月日: ${y}-${m}-${d},长度：${monthLen}`)

    this.time_title = this.formatDate(dd,'y年M月');
    let date = new Date(y,m-1,1);
    //该月1号周几
    let week_start = date.getDay();

    // 判断是否是周一开始
    if(this.mondayStart){
      week_start = (date.getDay() == 0 ? 7 : date.getDay()) - 1;
      this.weekList = this.setWeekList(this.weekList)
    }
    // 该月有多少周
    let week_len = Math.ceil((week_start + monthLen) / 7);
    let arr = [];
    let num = 1;
    let n_num = 1;
    for(var i = 0; i < week_len; i++){
      arr.push([])
      for(var j = 0; j < 7; j++){
        if((i < 1 && j < week_start)){
          // 上个月
          let l_d = l_monthLen - week_start + (1 + j);
          arr[i].push({
            year: l_y,
            month: l_m,
            day: l_d,
            time: `${l_y}-${l_m.toString().padStart(2,'0')}-${l_d.toString().padStart(2,'0')}`,
            label: 'last',
          });
        } else if((num > monthLen)){
          // 下个月
          arr[i].push({
            year: n_y,
            month: n_m,
            day: n_num,
            time: `${n_y}-${n_m.toString().padStart(2,'0')}-${n_num.toString().padStart(2,'0')}`,
            label: 'next',
          });
          n_num++;
        } else {
          // 本月
          arr[i].push({
            year: y,
            month: m,
            day: num,
            time: `${y}-${m.toString().padStart(2,'0')}-${num.toString().padStart(2,'0')}`,
            label: 'at',
          });
          num++;
        }
      }
    }
    lg.info(arr);
    this.dayList = arr;
  },

  // 点击<>切换月份
  cutMonth(num){
    lg.debug(this.today,'今天')
    let date = this.currentYM || this.today;
    let {year, month} = this.getDateInfo(date,num);
    this.currentYM = `${year}-${month.toString().padStart(2,'0')}`;
    lg.log('切换月份方式:',num, ', 结果:', this.currentYM)
    this.getCld(this.currentYM);
    this.$emit('cutMonth', this.currentYM, num)
  },

  // 获取上下个月的年份和月份方法
  getDateInfo(date,num = 0){
    let m_len = [31,28,31,30,31,30,31,31,30,31,30,31]; //月份的天数
    let dd = new Date(date);
    let y = dd.getFullYear();
    let m = dd.getMonth() + 1;
    let d = dd.getDate();
    if(m + num > 12){
      y = y + num;
      m = 1;
    } else if(m + num == 0) {
      y = y + num;
      m = 12;
    } else {
      m = m+num;
    }
    m_len[1] = (m == 2 && this.leapYear(y)) ? 29 : 28;
    return {year: y, month: m, day: d, monthLen: m_len[m - 1]};
  },

  // 格式化时间
  formatDate(date, fmt) {
    let ret;
    let o = {
      "y+": date.getFullYear(),
      "M+": date.getMonth() + 1,
      "d+": date.getDate(),
      "H+": date.getHours(),
      "m+": date.getMinutes(),
      "s+": date.getSeconds(),
      "W+": date.getDay(),
    };
    for (let k in o) {
      ret = new RegExp("(" + k + ")").exec(fmt);
      if (ret) {
        let str = o[k].toString();
        str = str.padStart(2,0);
        fmt = fmt.replace(ret[1], str);
      }
    }
    return fmt;
  },

  // 判断是否是闰年，是返回true，不是返回false
  leapYear(year) {
    if((year % 4 == 0 && year % 100 != 0) || year % 400 == 0){
      return true
    }
    return false;
  }
}

